package com.domain.VO;

import com.domain.DTO.FlightInfoDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 如果同时指定两个原则,默认选择money原则
 *
 * @author 大忽悠
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class BestFlightChoseRoleVo extends FlightChoseRoleVo {
    /**
     * 按照时间最短原则
     */
    private Boolean timeStrategy;
    /**
     * 按照money最少原则
     */
    private Boolean moneyStrategy;


    public FlightInfoDto getBestFlight(List<FlightInfoDto> flightInfoDtos) {
        //按照金钱策略
        if (getMoneyStrategy()) {
            FlightInfoDto bestFlight = flightInfoDtos.stream()
                    .sorted((flightInfoDto1, flightInfoDto2) ->
                            getPoorSeat() ? (int) (flightInfoDto1.getAggregateInfo().getTotalPoorPrice() - flightInfoDto2.getAggregateInfo().getTotalPoorPrice())
                                    : (int) (flightInfoDto1.getAggregateInfo().getTotalRichPrice() - flightInfoDto2.getAggregateInfo().getTotalRichPrice()))
                    .collect(Collectors.toList()).get(0);
            bestFlight.filterBestFlightClassByMoneyStrategy();
            return bestFlight;
        }
        //按照时间策略
        FlightInfoDto bestFlight = flightInfoDtos.stream().sorted((flightInfoDto1, flightInfoDto2) -> (int) (flightInfoDto1.getAggregateInfo().getTotalTime() - flightInfoDto2.getAggregateInfo().getTotalTime()))
                .collect(Collectors.toList()).get(0);
        bestFlight.filterBestFlightClassByTimeStrategy();
        return bestFlight;
    }

    public Boolean getTimeStrategy() {
        return timeStrategy == null ? Boolean.FALSE : Boolean.TRUE;
    }

    public Boolean getMoneyStrategy() {
        return moneyStrategy == null ? Boolean.FALSE : Boolean.TRUE;
    }
}
